package com.btex.app.treaty;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.btex.app.Config;
import com.btex.app.KLineActivity;
import com.btex.app.R;
import com.btex.app.entity.OrderBookInfo;
import com.btex.app.pager.BaseFragment;
import com.btex.app.treaty.adapter.KBuyAdapter;
import com.btex.app.treaty.adapter.KSellAdapter;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author zy zhangyi <zhangyi, 1104745049@QQ.com
 * @version v1.0
 * @project study1
 * @Description
 * @encoding UTF-8
 * @date 2018/10/26
 * @time 下午11:05
 * @修改记录 <pre>
 * 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 * </pre>
 */


//合约资产

public class KWeiTuoFragment extends BaseFragment {


    Unbinder unbinder;
    @BindView(R.id.fragment_wei_tuo_recy)
    RecyclerView mFragmentWeiTuoRecy;
    @BindView(R.id.fragment_wei_tuo_recy_one)
    RecyclerView mFragmentWeiTuoRecyOne;
    @BindView(R.id.input_buy_num_one)
    TextView mInputBuyNumOne;
    @BindView(R.id.input_buy_num_two)
    TextView mInputBuyNumTwo;
    private String mContract_id;
    private KBuyAdapter mBuyAdapter;
    private KSellAdapter mSellAdapter;
    private String mActivitytype;
    private boolean mHidden;
    private boolean mIsVisibleToUser;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_wei_tuo_item, null);
        unbinder = ButterKnife.bind(this, view);
        return view;

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        KLineActivity activity = (KLineActivity) getActivity();
        mContract_id = activity.mPair;
        mActivitytype = activity.activitytype;

        if (mActivitytype.equals("1")) {
            mInputBuyNumOne.setText(getString(R.string.input_buy_num));
            mInputBuyNumTwo.setText(getString(R.string.input_buy_num));
        }
        initRecy();
        getOrderbook();


        initListener();

    }

    private void initRecy() {

        mFragmentWeiTuoRecy.setLayoutManager(new LinearLayoutManager(getActivity()));
        mBuyAdapter = new KBuyAdapter();
        mFragmentWeiTuoRecy.setAdapter(mBuyAdapter);

        mFragmentWeiTuoRecyOne.setLayoutManager(new LinearLayoutManager(getActivity()));
        mSellAdapter = new KSellAdapter(getActivity());
        mFragmentWeiTuoRecyOne.setAdapter(mSellAdapter);

    }

    private void initListener() {


    }


    @Override
    public void setData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    public void getOrderbook() {

        int num = (int) ((Math.random() * 9 + 1) * 100000);
        RequestParams params = new RequestParams();
        params.addQueryStringParameter("pair", mContract_id);
        params.addQueryStringParameter("depth", "20");
        params.addHeader("myclient", "android");

        new HttpUtils().send(HttpRequest.HttpMethod.GET, Config.URL_ORDERBOOK + "?rnd=" + String.valueOf(num), params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                String result = responseInfo.result;
                Gson gson = new Gson();
                OrderBookInfo orderBookInfo = gson.fromJson(result, OrderBookInfo.class);
                ArrayList<OrderBookInfo.DataBean.BuyBean> buy = orderBookInfo.getData().getBuy();
                ArrayList<OrderBookInfo.DataBean.SellBean> sell = orderBookInfo.getData().getSell();
                try {

                    setDishList(buy, sell);

                } catch (IndexOutOfBoundsException e) {

                }


            }

            @Override
            public void onFailure(HttpException error, String msg) {


            }
        });

    }

    private void setDishList(ArrayList<OrderBookInfo.DataBean.BuyBean> buy, ArrayList<OrderBookInfo.DataBean.SellBean> sell) {


//        Gson gson = new Gson();
//        TreatyDishListInfo treatyDishListInfo = gson.fromJson(result, TreatyDishListInfo.class);
//        List<TreatyDishListInfo.DataBean.LongBean> longX = treatyDishListInfo.getData().getLongX();
//        List<TreatyDishListInfo.DataBean.ShortBean> shortX = treatyDishListInfo.getData().getShortX();
//

        Collections.sort(buy, new Comparator<OrderBookInfo.DataBean.BuyBean>() {
            @Override
            public int compare(OrderBookInfo.DataBean.BuyBean o1, OrderBookInfo.DataBean.BuyBean o2) {

                String amount = o1.getAmount();
                String amount1 = o2.getAmount();

                Double one = Double.valueOf(amount);
                Double two = Double.valueOf(amount1);


                if (one > two) {
                    return -1;
                } else if (one < two) {
                    return 1;
                } else {
                    return 0;
                }


            }
        });


        Collections.sort(sell, new Comparator<OrderBookInfo.DataBean.SellBean>() {
            @Override
            public int compare(OrderBookInfo.DataBean.SellBean o1, OrderBookInfo.DataBean.SellBean o2) {

                String amount = o1.getAmount();
                String amount1 = o2.getAmount();

                Double one = Double.valueOf(amount);
                Double two = Double.valueOf(amount1);


                if (one > two) {
                    return -1;
                } else if (one < two) {
                    return 1;
                } else {
                    return 0;
                }

            }
        });


        int max;

        String amount = sell.get(0).getAmount();
        String amount1 = buy.get(0).getAmount();
        if (Double.valueOf(amount) > Double.valueOf(amount1)) {
            double ceil = Math.ceil(Double.valueOf(amount));
            max = (int) ceil;
        } else {
            double ceil = Math.ceil(Double.valueOf(amount1));
            max = (int) ceil;
        }


        Collections.sort(buy, new Comparator<OrderBookInfo.DataBean.BuyBean>() {
            @Override
            public int compare(OrderBookInfo.DataBean.BuyBean o1, OrderBookInfo.DataBean.BuyBean o2) {

                String amount = o1.getPrice();
                String amount1 = o2.getPrice();

                Double one = Double.valueOf(amount);
                Double two = Double.valueOf(amount1);


                if (one > two) {
                    return -1;
                } else if (one < two) {
                    return 1;
                } else {
                    return 0;
                }


            }
        });

        mBuyAdapter.setMax(max);
        mBuyAdapter.setNewData(sell);


        Collections.sort(sell, new Comparator<OrderBookInfo.DataBean.SellBean>() {
            @Override
            public int compare(OrderBookInfo.DataBean.SellBean o1, OrderBookInfo.DataBean.SellBean o2) {

                String amount = o1.getPrice();
                String amount1 = o2.getPrice();

                Double one = Double.valueOf(amount);
                Double two = Double.valueOf(amount1);


                if (one > two) {
                    return 1;
                } else if (one < two) {
                    return -1;
                } else {
                    return 0;
                }

            }
        });

        mSellAdapter.setMax(max);

        mSellAdapter.setNewData(buy);

//
//
//        mBuyAdapter.setNewData(shortX);
//        mSellAdapter.setNewData(longX);
//
//        mFragmentWeiTuoRecy.setAdapter(mBuyAdapter);
//        mFragmentWeiTuoRecyOne.setAdapter(mSellAdapter);


    }


    private Handler handler = new Handler() {

    };
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {

            if (mIsVisibleToUser) {

                getOrderbook();

            }

            handler.postDelayed(this, 5000);

        }
    };


    @Override
    public void onResume() {
        super.onResume();

        handler.postDelayed(runnable, 0);
    }

//    @Override
//    public void onHiddenChanged(boolean hidden) {
//        mHidden = hidden;
//        super.onHiddenChanged(hidden);
//
//    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {

        super.setUserVisibleHint(isVisibleToUser);
        mIsVisibleToUser = isVisibleToUser;
    }

    @Override
    public void onPause() {
        super.onPause();
        mIsVisibleToUser = false;
    }
}
